package com.then.component.club.applike;

import android.app.Activity;
import android.app.Application;

import com.luojilab.component.componentlib.applicationlike.IApplicationLike;
import com.luojilab.component.componentlib.router.Router;
import com.luojilab.component.componentlib.router.ui.UIRouter;
import com.luojilab.componentservice.app.DaggerComponentService;
import com.then.architecture.InjectorMap;
import com.then.architecture.di.DaggerComponent;
import com.then.basic.socket.ThreadUtil;
import com.then.component.club.BuildConfig;
import com.then.component.club.di.AppLikeComponent;
import com.then.component.club.di.ClubModule;
import com.then.component.club.di.DaggerAppLikeComponent;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;

/**
 * Created by 42524 on 2018/1/27.
 */

public class ClubAppLike implements IApplicationLike, HasActivityInjector {


    public static final String DB_NAME = "area_new.db";

    private   String dbPath = "";



    UIRouter uiRouter = UIRouter.getInstance();

    @Inject
    DispatchingAndroidInjector<Activity> mActivityInjector;

    @Inject
    InjectorMap injectorMap;


    AppLikeComponent appLikeComponent;

    @Override
    public void onCreate() {
        uiRouter.registerUI("club");
        Object o = Router.getInstance().getService(DaggerComponentService.class.getSimpleName());
        if (o != null) {
            DaggerComponent component = (DaggerComponent) o;
            appLikeComponent = DaggerAppLikeComponent.builder().daggerComponent(component).clubModule(new ClubModule(this)).build();
            appLikeComponent.inject(this);
            injectorMap.putActivityInjector(BuildConfig.APPLICATION_ID, this);
            try {
                copyDataBase(component.application());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    private void copyDataBase(Application ctx) throws IOException {
        dbPath=ctx.getDatabasePath(DB_NAME).getPath();
        ThreadUtil.async(() ->
                { InputStream mInput = null;
                    OutputStream mOutput = null;
                    try {
                        mInput = ctx.getAssets().open(DB_NAME);
                        String outfileName = dbPath;
                        mOutput = new FileOutputStream(outfileName);
                        byte[] buffer = new byte[1024];
                        int mLength;
                        while ((mLength = mInput.read(buffer)) > 0) {
                            mOutput.write(buffer, 0, mLength);
                        }
                        mOutput.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (mInput != null) {
                            try {
                                mInput.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (mOutput != null) {
                            try {
                                mOutput.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
        );

    }


    @Override
    public void onStop() {
        uiRouter.unregisterUI("club");
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return mActivityInjector;
    }
}
